package yl.hs.bmipfull.service.material.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.material.IPurchaseSystemLogDao;
import yl.hs.bmipfull.daos.material.IRawMaterialInventoryDao;
import yl.hs.bmipfull.pojo.material.PurchaseSystemLogEntity;
import yl.hs.bmipfull.service.material.IPurchaseSystemLogService;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;

import java.util.ArrayList;
import java.util.List;

/**
 * 作用:   <br/>
 * 创建时间: 2021-07-08 17:15 <br/>
 *
 * @author 吴高丰
 */
@Service
public class PurchaseSystemLogServiceImpl extends PurchaseBaseService implements IPurchaseSystemLogService {
    @Autowired
    IPurchaseSystemLogDao dao;
    @Autowired
    IRawMaterialInventoryDao inventoryDao;
    @Override
    public List<PurchaseSystemLogEntity> queryLsitByDataSn(String dataSN) {
        return dao.queryLsitByDataSn(dataSN);
    }

    @Override
    public PageResult<Object> queryCP(PageQuery pageQuery, int rowNum, int page) {
        try {
            pageQuery.checkSortSides(PurchaseSystemLogEntity.class);
            pageQuery.getQuery().put("querySkip", (page - 1) * rowNum);
            pageQuery.getQuery().put("pageSize", rowNum);
            PageResult<Object> result = new PageResult(rowNum);
            pageQuery.getQuery().put("unitSNArray", getCompanySNList());
            result.setRecords(dao.queryCountByConditions(pageQuery.getQuery()));
            result.setPage(page);
            var data = dao.queryByEntity(pageQuery.getQuery());
            List<Object> datas = new ArrayList<>();
            data.forEach(s -> {
                var results = s.toMap();
                var inventory = inventoryDao.findBySN(s.getDataSN());
                if (inventory != null) {
                    results.put("rmCateName", inventory.getRmCateName());
                    results.put("rmName", inventory.getRmName());
                    results.put("rmSpec", inventory.getRmSpec());
                    results.put("whName", inventory.getWhName());
                    datas.add(results);
                }
            });
            result.setData(datas);
            return result;
        }catch (Exception ee){
            throw ee;
        }
    }

    @Override
    public Integer writeLogs(ActionType action, String dataSN, String content, String dataJson, Class<?> classType) {
        return null;
    }
}